package guis.visitant;

import exceptions.RepositoryException;
import guis.RegisterPanel;
import guis.TablePanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import classes.Iterator;
import classes.Visitant;

import communication.Facade;

public class VisitantPanel extends RegisterPanel< Visitant >
{
	private static final long		serialVersionUID	= -2238677678739841723L;

	public VisitantPanel( JFrame parent, Facade facade )
	{
		super( parent, facade );
		this.createTables();
		this.createActions();
	}

	private void createTables()
	{
		this.tablePanel = new TablePanel< Visitant >( 10, 10, 620, 530 );
		formPanel.add( tablePanel );
		tablePanel.setHeader( new Visitant( "", null, 0 ) );
		loadTable( null );
	}

	private void createActions()
	{
		insertButton.addActionListener( new ActionListener()
		{

			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				new InsertVisitantWindow( parent, self, facade );
			}

		} );

		editButton.addActionListener( new ActionListener()
		{

			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				new EditVisitantWindow( parent, self, facade );
			}

		} );

		findButton.addActionListener( new ActionListener()
		{

			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				new FindVisitantWindow( parent, self, facade );
			}

		} );

		removeButton.addActionListener( new ActionListener()
		{

			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				Object[] options = { "      Sim      ", "      Não      " };
				int result = JOptionPane.showOptionDialog( null, "Deseja deletar o visitante selecionado?", "Deleção", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options,
						options[ 1 ] );

				if ( result == JOptionPane.YES_OPTION )
				{
					try
					{
						JTable table = tablePanel.getTable();
						int selectedIndex = table.getSelectedRow();
						facade.removeVisitant( Integer.parseInt( ( String ) table.getModel().getValueAt( selectedIndex, 0 ) ) );
						tablePanel.remove();
					}
					catch ( NumberFormatException e )
					{
						e.printStackTrace();
					}
					catch ( RepositoryException e )
					{
						e.printStackTrace();
					}
				}
			}

		} );

		tablePanel.getTable().getSelectionModel().addListSelectionListener( new ListSelectionListener()
		{

			@Override
			public void valueChanged( ListSelectionEvent arg0 )
			{
				JTable table = tablePanel.getTable();
				if ( table.getSelectionModel().getMaxSelectionIndex() >= 0 && table.getRowCount() > 0 )
				{
					editButton.setEnabled( true );
					removeButton.setEnabled( true );
				}
				else
				{
					editButton.setEnabled( false );
					removeButton.setEnabled( false );
				}
			}

		} );

	}

	public void loadTable( Iterator< Visitant > iterator )
	{
		try
		{
			DefaultTableModel dtm = ( DefaultTableModel ) tablePanel.getTable().getModel();
			while ( dtm.getRowCount() > 0 )
			{
				dtm.removeRow( 0 );
			}

			if ( iterator == null )
				iterator = facade.getIteratorVisitant();
			while ( iterator.hasNext() )
			{
				tablePanel.addRow( iterator.next() );
			}
		}
		catch ( RepositoryException e )
		{
		}
	}
}